﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Content;
using System.Globalization;

namespace Comp476Project
{
    class Map
    {
        public Model grass, path, ground;
        public Matrix projection, world;
        public float size;
        public int mapSize = 120;
        public float[][] grid;
        public bool drawNode;
        public List<Node> midPath;
        public List<Node> leftPath;
        public List<Node> rightPath;

        public Map(ref Matrix projection, ContentManager Content, GraphicsDevice g)
        {
            drawNode = false;
            grid = new float[mapSize][];
            gridFill();
            grass = Content.Load<Model>("map/mapgrass");
            path = Content.Load<Model>("map/mappath");
            ground = Content.Load<Model>("map/mapbase");
            size = 1.0f;
            this.projection = projection;
            world = Matrix.CreateScale(size) * Matrix.CreateRotationX(MathHelper.ToRadians(-90f)) * Matrix.CreateTranslation(new Vector3(0, 0, 0));
            readFile();

            //======================================Creating paths======================================//

            midPath = new List<Node>();
            leftPath = new List<Node>();
            rightPath = new List<Node>();

            //computing middle path
           // midPath.Add(new Node(new Vector3(0.001f,0.06f,-0.698f),projection,g,Color.Green)); //HQ 1
            midPath.Add(new Node(new Vector3(0.002f,0.06f,-0.749f), projection, g, Color.Green));
            midPath.Add(new Node(new Vector3(0.038f, 0.05f, -0.444f), projection, g, Color.Green));
            midPath.Add(new Node(new Vector3(0.007f, 0.04f, -0.318f), projection, g, Color.Green));
            midPath.Add(new Node(new Vector3(0.001f, 0.04f, -0.184f), projection, g, Color.Green));
            midPath.Add(new Node(new Vector3(0.001f, -0.06f, -0.01f), projection, g, Color.Green));
            midPath.Add(new Node(new Vector3(0.003f, 0.018f, 0.126f), projection, g, Color.Green));
            midPath.Add(new Node(new Vector3(0.002f, 0.016f, 0.304f), projection, g, Color.Green));
            midPath.Add(new Node(new Vector3(0.005f, 0.06f, 0.486f), projection, g, Color.Green));
            midPath.Add(new Node(new Vector3(-0.001f, 0.06f, 0.773f), projection, g, Color.Green));
            //midPath.Add(new Node(new Vector3(0.00f, 0.06f, 0.698f), projection, g, Color.Green));// HQ 2

            //leftPath.Add(new Node(new Vector3(0.001f, 0.06f, -0.698f), projection, g, Color.Blue));
            leftPath.Add(new Node(new Vector3(0.002f,0.06f,-0.749f), projection, g, Color.Blue));
            leftPath.Add(new Node(new Vector3(0.148f, 0.06f, -0.521f), projection, g, Color.Blue));
            leftPath.Add(new Node(new Vector3(0.34f, 0.06f, -0.366f), projection, g, Color.Blue));
            leftPath.Add(new Node(new Vector3(0.49f, 0.06f, -0.204f), projection, g, Color.Blue));
            leftPath.Add(new Node(new Vector3(0.534f, -0.009f, 0f), projection, g, Color.Blue));
            leftPath.Add(new Node(new Vector3(0.484f, 0.06f, 0.18f), projection, g, Color.Blue));
            leftPath.Add(new Node(new Vector3(0.351f, 0.06f, 0.348f), projection, g, Color.Blue));
            leftPath.Add(new Node(new Vector3(0.188f, 0.06f, 0.539f), projection, g, Color.Blue));
            leftPath.Add(new Node(new Vector3(-0.001f, 0.06f, 0.773f), projection, g, Color.Blue));
            //leftPath.Add(new Node(new Vector3(0.00f, 0.06f, 0.698f), projection, g, Color.Green));

            //rightPath.Add(new Node(new Vector3(0.001f, 0.06f, -0.698f), projection, g, Color.Red));
            rightPath.Add(new Node(new Vector3(0.002f,0.06f,-0.749f), projection, g, Color.Red));
            rightPath.Add(new Node(new Vector3(-0.158f, 0.06f, -0.52f), projection, g, Color.Red));
            rightPath.Add(new Node(new Vector3(-0.352f, 0.06f, -0.36f), projection, g, Color.Red));
            rightPath.Add(new Node(new Vector3(-0.517f, 0.06f, -0.13f), projection, g, Color.Red));
            rightPath.Add(new Node(new Vector3(-0.536f, -0.009f, 0f), projection, g, Color.Red));
            rightPath.Add(new Node(new Vector3(-0.501f, 0.06f, 0.163f), projection, g, Color.Red));
            rightPath.Add(new Node(new Vector3(-0.327f, 0.06f, 0.387f), projection, g, Color.Red));
            rightPath.Add(new Node(new Vector3(-0.187f, 0.06f, 0.554f), projection, g, Color.Red));
            rightPath.Add(new Node(new Vector3(-0.001f, 0.06f, 0.773f), projection, g, Color.Red));
            //rightPath.Add(new Node(new Vector3(0.00f, 0.06f, 0.698f), projection, g, Color.Green));


            //computing left path



            //==========================================================================================//


            //print();
        }

        public void update()
        {

        }

        public void Draw(Matrix view)
        {
            if (drawNode)
            {
                foreach (Node n in midPath)
                {
                    n.draw(view);
                }
                foreach (Node n in leftPath)
                {
                    n.draw(view);
                }
                foreach (Node n in rightPath)
                {
                    n.draw(view);
                }
            }
            DrawModel(grass, world, view, projection);
            DrawModel(path, world, view, projection);
            DrawModel(ground, world, view, projection);
        }

        private void DrawModel(Model model, Matrix world, Matrix view, Matrix projection)
        {
            foreach (ModelMesh mesh in model.Meshes)
            {
                foreach (BasicEffect effect in mesh.Effects)
                {
                    effect.EnableDefaultLighting();
                    //effect.DirectionalLight0.Direction = 1.5f * arena.sun.direction;
                    effect.DirectionalLight0.Direction = 1.0f * new Vector3(-1, -1, -1);
                    effect.PreferPerPixelLighting = true;
                    //effect.DirectionalLight0.DiffuseColor = new Vector3(1.0f, 1.0f, 1.0f);
                    //effect.DirectionalLight0.Direction = new Vector3(-1, -1, -1);
                    //effect.DirectionalLight0.SpecularColor = new Vector3(1.0f, 1.0f, 1.0f);
                    //effect.AmbientLightColor = new Vector3(0.2f, 0.2f, 0.2f);
                    //effect.EmissiveColor = new Vector3(0.1f, 0.1f, 0.1f);
                    //effect.DirectionalLight0.Enabled = false;
                    //effect.LightingEnabled = true;
                    effect.World = world;
                    effect.View = view;
                    effect.Projection = projection;
                    //effect.Alpha = 0.7f;
                }

                mesh.Draw();
            }
        }

        private void readFile() 
        {
            string line;
            int counter = 1;
            float y;
            int x = 0;
            int z = 0;
            
            // Read the file and display it line by line.
            System.IO.StreamReader file = new System.IO.StreamReader("hmap.txt");
            //System.IO.StreamWriter file2 = new System.IO.StreamWriter("c:\\test.txt");
            
            //Loop reading file and storing y values in grid array
            while ((line = file.ReadLine()) != null)
            {
                if ( counter % 3 == 0)
                {
                    y = float.Parse(line, NumberStyles.AllowLeadingSign | NumberStyles.AllowTrailingWhite | NumberStyles.AllowDecimalPoint);
                    grid[x][z] = y;
                    counter = 0;
                    //file2.WriteLine(y);

                }
                else if ( counter % 2 == 0)
                {
                    double tempZ = double.Parse(line, NumberStyles.AllowLeadingSign | NumberStyles.AllowTrailingWhite | NumberStyles.AllowDecimalPoint);
                    z = (int)Math.Round(((tempZ - (-1)) * mapSize) / 2);
                    if (z >=mapSize)
                        z=mapSize-1;
                }
                else 
                {
                    double tempX = double.Parse(line, NumberStyles.AllowLeadingSign | NumberStyles.AllowTrailingWhite | NumberStyles.AllowDecimalPoint);
                    x = (int)Math.Round(((tempX - (-1)) * mapSize) / 2);
                    if (x >=mapSize)
                        x=mapSize-1;
                }
                
                counter++;
                
            }

            file.Close();
            //Console.WriteLine(counter);
        }//end of read file function

        //public void print() 
        //{
        //    int count = 0;
        //    float max = grid[0][0];
        //    float min = grid[0][ 0];
        //    foreach(float f in grid)
        //    {
        //        if (f != 0)
        //        {
        //            count++;
        //            Console.WriteLine(f);

        //        }
        //        if (f >= 9)
        //            count++;
        //        if (f > max)
        //            max = f;
        //        if (f < min)
        //            min = f;
        //    }
        //    Console.WriteLine("count is: "+count);
        //    //Console.WriteLine("min y is: "+min);
        //    //Console.WriteLine("max y is: " + max);
        //}
        public void gridFill() 
        {
            for (int i = 0; i < mapSize; ++i)
            {
                grid[i] = new float[mapSize];
                for (int j = 0; j < mapSize;++j )
                {
                    grid[i][j] =0f;
                }
            }
            
           
        }//end of fill
    }
}
